/**
 * Sistem Informasi Penjualan
 * @author Kelompok 1
 * Ligar Sekar Wangi
 * Tri Maryani
 * Winda Rahadyan
 * TIF/IV/A
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.sql.*;
import java.lang.*;
import javax.swing.*;
import java.util.*;
import java.text.*;

public class ReportFaktur extends JInternalFrame implements ActionListener 
 {
	private JButton btnCetak1;
	private JTextField txtNo_Faktur;
	private JComboBox cboNo_Faktur;
	private JLabel lblNo_Faktur;
	private String[] arrMF;
	private String[][] arrDF;
	private String sql1;
	private ResultSet rs1;
	private int jmlRecRinci,MTot;
				
	ReportFaktur()
	{
	/* super(Judul, UkuranFrame, TombolClose, TombolMaksimum, TombolMinimum)*/
		super ("Print Report Faktur", false, true, false, true);
		/* mengatur ukuran frame */
		setSize (350, 100);
		
		lblNo_Faktur = new JLabel("No. Faktur");
		lblNo_Faktur.setBounds(20, 20, 80, 25);
		cboNo_Faktur = new JComboBox();
		cboNo_Faktur.setBounds(110, 20, 120, 25);

		txtNo_Faktur = new JTextField("F00000001");
		txtNo_Faktur.setBounds(110, 20, 120, 25);

		btnCetak1 = new JButton("Print");
		btnCetak1.setBounds(250, 20, 80, 25);
		btnCetak1.addActionListener(this);
		
		getContentPane().setLayout(null);
		getContentPane().add (lblNo_Faktur);
		getContentPane().add (txtNo_Faktur);
		getContentPane().add (btnCetak1);

		setVisible (true);
	}
	
	public void actionPerformed (ActionEvent ae) 
	  {
		if (ae.getSource()==btnCetak1) 
		  {
			cariMF();
			HitungRinci();
			cariDF();
			
			/* Setting Print */
			PrinterJob PrintOK1 = PrinterJob.getPrinterJob();
			PrintOK1.setPrintable(new isiFaktur(arrMF, arrDF, jmlRecRinci, MTot));
			if (PrintOK1.printDialog()) 
			 {
				try
				 {
					PrintOK1.print();
				 }
				catch (PrinterException e) 
				 {
					System.out.println(e);
				 }
			}
		}
	}
	
	String cariMF()
	 {
		arrMF = new String[4];
		try 
		 {
			/* Koneksi Database dan buka Tabel Faktur */
			Connect ObjKoneksi1 = new Connect();
			Connection con1 = ObjKoneksi1.OpenConnect();
			Statement st1 = con1.createStatement();
			sql1 = "select Faktur.No_Faktur, Faktur.Kode_Pelanggan, Pelanggan.Nama_Pelanggan, Faktur.tgl_Jual from Faktur, Pelanggan where Faktur.No_Faktur='"+txtNo_Faktur.getText()+"' and Faktur.Kode_Pelanggan=Pelanggan.Kode_Pelanggan" ;
			rs1 = st1.executeQuery(sql1);
			if (rs1.next())
				{
				arrMF[0] = rs1.getString(1);
				arrMF[1] = rs1.getString(2);
				arrMF[2] = rs1.getString(3);
				arrMF[3] = rs1.getString(4).substring(0,10);
			}
			rs1.close();
			con1.close();
		}
		catch (Exception e) 
		{
			System.out.println(e);
		}
		return "";
	}
		
	String HitungRinci()
	 {
		try
		 {
			Connect ObjKoneksi1 = new Connect();
			Connection con1= ObjKoneksi1.OpenConnect();
			Statement st1 = con1.createStatement();
			sql1 = "SELECT * FROM Transj where No_Faktur='"+txtNo_Faktur.getText()+"'";
			rs1 = st1.executeQuery(sql1);
			jmlRecRinci=0;
			while (rs1.next())
			{
				jmlRecRinci++;
			}
			rs1.close();
			con1.close();
		}
		catch(Exception e) 
		{
		}
		return "";
	}
	
	String cariDF()
		{
		int i=0;
		arrDF = new String[jmlRecRinci][5];
		try 
		 {
			/* buka database dan tabel Transj */
			Connect ObjKoneksi1 = new Connect();
			Connection con1 = ObjKoneksi1.OpenConnect();
			Statement st1 = con1.createStatement();
			sql1 = "select Transj.Kode_Barang, Barang.Nama_Barang, Transj.Harga_Jual, Transj.Qty_Jual from Transj, Barang where Transj.No_Faktur='"+txtNo_Faktur.getText()+"' and Transj.Kode_Barang=Barang.Kode_Barang" ;
			rs1 = st1.executeQuery(sql1);
			
			while (rs1.next())
			{
				arrDF[i][0] = rs1.getString(1);
				arrDF[i][1] = rs1.getString(2);
				arrDF[i][2] = rs1.getString(3);
				arrDF[i][3] = rs1.getString(4);
				i++;
			}
			
			rs1.close();			con1.close();

			MTot = 0;
			
			/* Isi Mtot Ke Variabel Array arrDF[] */
			for (int j=0; j<jmlRecRinci; j++)
			{
				String hrg1 = arrDF[j][2];
				String jml1 = arrDF[j][3];
				int tot1 = Integer.parseInt(hrg1) * Integer.parseInt(jml1);
				arrDF[j][4] = ""+tot1;
				MTot += tot1;
			}
		}
		catch (Exception e) 
		{
			System.out.println(e);
		}
		return "";
	}
	
}	

class isiFaktur implements Printable {
	private java.util.Date currDate = new java.util.Date ();
	private SimpleDateFormat sdf1 = new SimpleDateFormat ("dd MMMM yyyy", Locale.getDefault());
	private String d1 = sdf1.format (currDate) ;
	private String[] cetakMF;
	private String[][] cetakDF;
	private	int i1, x1, y1, jR1, MT1;
	
	public isiFaktur(String[] a1, String[][] b1, int jmlRec1,int MTot)
	{
		cetakMF=a1;
		cetakDF=b1;
		jR1=jmlRec1;
		MT1=MTot;
	}
	
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException 
	 {
		try
		 {
			Graphics2D g1 = (Graphics2D) graphics;
			g1.setFont(new Font("Arial", Font.ITALIC, 12));
			g1.setColor(Color.black);
				
			/* Buat Header (Judul) Laporan Faktur */	
			g1.drawString("DAFTAR PENJUALAN BARANG",230,100);
			g1.drawString("Kelompok 1 ",280,115);

			g1.drawLine(100,125,550,125);
			x1 = 110;
			y1 = 135;
			
			/* Isi Faktur */
			g1.drawString("No. Faktur", 110, y1+15);
			g1.drawString(": " + cetakMF[0], 220, y1+15);
			g1.drawString("Kode Pelanggan", 110, y1+30);
			g1.drawString(": " + cetakMF[1], 220, y1+30);
			g1.drawString("Nama Pelanggan", 110, y1+45);
			g1.drawString(": " + cetakMF[2], 220, y1+45);
			g1.drawString("Tgl. Jual", 110, y1+60);
			g1.drawString(": " + cetakMF[3], 220, y1+60);
			
			g1.drawLine(100,y1+80,550,y1+80);
			y1 += 95;

			/* Print Header Rinci Faktur */
			x1 = 100;
			g1.drawString("No.", x1+10, y1);
			g1.drawString("Kode", x1+50, y1);
			g1.drawString("Nama Barang", x1+100, y1);
			g1.drawString("Harga", x1+300, y1);
			g1.drawString("Qty", x1+350, y1);
			g1.drawString("Jumlah", x1+380, y1);
			y1 +=12;

			g1.drawLine(100,y1,550,y1);
			y1 +=15;
			
			/* Print Rinci Faktur */
			for ( int i1 =0; i1<jR1; i1++ ) 
			 {
				g1.drawString(""+(i1+1)+".", x1+10, y1);
				g1.drawString(cetakDF[i1][0], x1+45, y1);
				g1.drawString(cetakDF[i1][1], x1+110, y1);
				g1.drawString(cetakDF[i1][2], x1+315, y1);
				g1.drawString(cetakDF[i1][3], x1+360, y1);
				g1.drawString(cetakDF[i1][4], x1+375, y1);
				y1 +=15;
			}
			
			g1.drawLine(100,y1,550,y1);
			/* Print Jumlah */
			g1.drawString("Total : " ,150, y1+20);
			g1.drawString("" +MT1,475, y1+20);
			g1.drawString("Tanggal Cetak : "+d1,100, y1+80);
		}
		catch (Exception e)	
		{
		}
		return PAGE_EXISTS;
	}
	
} 